
CC=/cross/i386-linux-elf/gcc-core-static/bin/i386-lenxyang-elf-gcc
LD=/cross/i386-linux-elf/gcc-core-static/bin/i386-lenxyang-elf-ld

image=vm/a.img
out=out

lib=lib
i386=i386
boot=i386/boot
i386mach=i386/mach-default
linux=linux

cflags =  -g  -I. -Wall -Wstrict-prototypes \
              -march=i386 -mregparm=3 \
              -fno-strict-aliasing -fomit-frame-pointer \
              -ffreestanding \
              -fno-toplevel-reorder\
              -fno-unit-at-a-time \
              -fno-stack-protector \
              -mpreferred-stack-boundary=2 \
              -mno-sse -mno-mmx -mno-sse2 -mno-3dnow \
              -pipe -m32 -mtune=i386 \
              -std=gnu99 \
	      -D__KERNEL__ \
              -DCONFIG_X86_32 

aflags =  ${cflags} -D__ASSEMBLY__

system_objs = out/${i386}/system/main16.c.o \
		out/${i386}/system/string.c.o \
		out/${i386}/system/screen.c.o \
		out/${i386}/system/head.S.o \
		out/${i386}/system/pm.c.o \
	        out/${i386}/system/pmjump.S.o \

lib_objs = out/${lib}/string.c.o \
	out/${lib}/ctype.c.o  \
	out/${lib}/vsprintf.c.o

i386kernel_objs= out/${i386}/kernel/head.S.o \
		out/${i386}/kernel/entry.S.o \
		out/${i386}/kernel/kernel.c.o \
		out/${i386}/kernel/screen.c.o \
		out/${i386}/kernel/trap.c.o \
		out/${i386}/kernel/i8259.c.o \
		out/${i386}/kernel/time.c.o \
		out/${i386}/kernel/irq.c.o \
		out/${i386}/kernel/early_printk.c.o \
		out/${i386}/kernel/timers/timer_pit.c.o \
		out/${i386}/kernel/timers/timer_none.c.o \
		out/${i386mach}/setup.c.o \
		out/${i386}/mm/init.c.o

#		out/${i386}/kernel/printf.c.o \

linux_objs = out/${linux}/timer.c.o \
		out/${linux}/main.c.o \
		out/${linux}/printk.c.o \
		out/${linux}/irq/handle.c.o \
		out/${linux}/irq/proc.c.o \
		out/${linux}/irq/manage.c.o

default: image

binary = out/${boot}/bootsect.bin \
	 out/${i386}/system.bin

image : ${binary}
	dd if=out/${boot}/bootsect.bin of=${image} bs=512 count=1 conv=notrunc seek=0
	dd if=out/${i386}/system.bin of=${image} bs=512 count=50 conv=notrunc seek=1

out/${boot}/bootsect.bin: out/${boot}/bootsect.S.o
	${LD} --oformat binary -Ttext 0x7c00 $< -o $@
	${LD} -N -Ttext 0x7c00 $< -o out/${boot}/bootsect.gdb

out/${boot}/bootsect.S.o : ${boot}/bootsect.S
	${CC} ${aflags} -g -c $< -o $@

out/${i386}/system.bin: ${system_objs} ${i386kernel_objs} ${lib_objs} ${linux_objs} ${i386}/system.ld
	${LD} -s -N -T ${i386}/system.ld $? -o $@
	${LD} -N -T ${i386}/system.ld $? -o out/${i386}/system.gdb

out/${i386}/system/head.S.o: ${i386}/system/head.S
	${CC} ${aflags} -c $< -o $@

out/${i386}/system/pmjump.S.o: ${i386}/system/pmjump.S
	${CC} ${aflags} -c $< -o $@

out/${i386}/system/pm.c.o: ${i386}/system/pm.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/system/main16.c.o : ${i386}/system/main16.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/system/screen.c.o: ${i386}/system/screen.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/system/string.c.o: ${i386}/system/string.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/head.S.o : ${i386}/kernel/head.S
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/entry.S.o : ${i386}/kernel/entry.S
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/kernel.c.o : ${i386}/kernel/kernel.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/trap.c.o : ${i386}/kernel/trap.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/printf.c.o : ${i386}/kernel/printf.c
	${CC} ${cflags} -c $< -o $@

out/${lib}/string.c.o : ${lib}/string.c
	${CC} ${cflags} -c $< -o $@

out/${lib}/vsprintf.c.o : ${lib}/vsprintf.c
	${CC} ${cflags} -c $< -o $@

out/${lib}/ctype.c.o : ${lib}/ctype.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/screen.c.o : ${i386}/kernel/screen.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/i8259.c.o : ${i386}/kernel/i8259.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/timers/timer_pit.c.o : ${i386}/kernel/timers/timer_pit.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/timers/timer_none.c.o : ${i386}/kernel/timers/timer_none.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/time.c.o: ${i386}/kernel/time.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/irq.c.o: ${i386}/kernel/irq.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/kernel/early_printk.c.o : ${i386}/kernel/early_printk.c
	${CC} ${cflags} -c $< -o $@

out/${i386mach}/setup.c.o : ${i386mach}/setup.c
	${CC} ${cflags} -c $< -o $@

out/${i386}/mm/init.c.o : ${i386}/mm/init.c
	${CC} ${cflags} -c $< -o $@

out/${linux}/timer.c.o : ${linux}/timer.c
	${CC} ${cflags} -c $< -o $@

out/${linux}/main.c.o : ${linux}/main.c
	${CC} ${cflags} -c $< -o $@

out/${linux}/printk.c.o : ${linux}/printk.c
	${CC} ${cflags} -c $< -o $@

out/${linux}/irq/handle.c.o : ${linux}/irq/handle.c
	${CC} ${cflags} -c $< -o $@

out/${linux}/irq/manage.c.o: ${linux}/irq/manage.c
	${CC} ${cflags} -c $< -o $@

out/${linux}/irq/proc.c.o: ${linux}/irq/proc.c
	${CC} ${cflags} -c $< -o $@


# 清理生成的文件
clean:
	rm -fr out
	mkdir -p out
	mkdir -p out/${boot}
	mkdir -p out/${lib}
	mkdir -p out/${i386}/system
	mkdir -p out/${i386}/kernel
	mkdir -p out/${i386}/kernel/timers
	mkdir -p out/${i386}/mm
	mkdir -p out/${i386mach}
	mkdir -p out/${linux}
	mkdir -p out/${linux}/irq

gdb:
	tools/gdb.sh

run:
	tools/run.sh

bgdb:
	tools/bochs_gdb.sh
